home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection 1998 Fall: Game Toolkit / Disc.iso / Samples / Moofwars 1.02 / MoofWars Encoder / •Sources / PICTMask.cp < prev   
Encoding:
Text File  |  1998-02-23  |  6.8 KB  |  218 lines  |  [TEXT/CWIE]

  1. /*************************************************************************************
  2. #
  3. #    PICTMask.cp
  4. #
  5. #    Author: Timothy Carroll
  6. #    Apple Developer Technical Support
  7. #    timc@apple.com
  8. #
  9. #    Modification History: 
  10. #
  11. #    8/15/96        TMC     Initial Release
  12. #
  13. #    2/24/97        TMC        Now explicitly include main.h
  14. #    2/23/98        TMC        When we created the PICT, we weren't setting the clip rectangle.
  15. #   The default clipping region would cause problems if we ever scaled the PICT when we
  16. #   draw it.
  17. #
  18. #    Copyright © 1996 Apple Computer, Inc., All Rights Reserved
  19. #
  20. #
  21. #    You may incorporate this sample code into your applications without
  22. #    restriction, though the sample code has been provided "AS IS" and the
  23. #    responsibility for its operation is 100% yours.  However, what you are
  24. #    not permitted to do is to redistribute the source as "DSC Sample Code"
  25. #    after having made changes. If you're going to re-distribute the source,
  26. #    we require that you make it clear in the source that the code was
  27. #    descended from Apple Sample Code, but that you've made changes.
  28. #
  29. *************************************************************************************/
  30.  
  31. #include "Main.h"
  32. #include "Color Search Procs.h"
  33. #include "PICTMask.h"
  34.  
  35. OSStatus GeneratePICTMasks (short inputFileResNum, short outputFileResNum)
  36. {
  37.     OSStatus        theErr;
  38.  
  39.     UInt16            numPicts, loop;
  40.     
  41.     // we pass these to GetResInfo so that we can get the actual resource ID.
  42.     SInt16            resID;
  43.     ResType            resType;
  44.     Str255            resName;
  45.  
  46.     // Holds the last value on the resource chain since we change the top resource a lot
  47.     SInt16            saveResNum;
  48.     
  49.     // The current picture being worked on, along with its GWorld and Pixmap
  50.     PicHandle        thePicture = NULL;
  51.     GWorldPtr        pictGWorld = NULL;
  52.     PixMapHandle    pictPixMap = NULL;
  53.     OpenCPicParams    theParams; // used to build the output picture
  54.     Rect            pictRect, drawRect, sourceRect;
  55.     
  56.     // Saves port info while we draw into the GWorld
  57.     CGrafPtr        savePort;
  58.     GDHandle         saveDevice;
  59.     
  60.     SInt16            offset;
  61.     
  62.     Boolean            pictIsResource = false; // Determines if we call ReleaseResource or DisposeHandle
  63.     
  64.     // Save off the current resource chain so that we can restore it later
  65.     saveResNum = CurResFile();
  66.  
  67.     UseResFile (inputFileResNum);
  68.  
  69.  
  70.  
  71.     // First thing is to copy the color table resource to the output.
  72.     theErr = CopyResource (inputFileResNum, outputFileResNum,'clut', kAppColorTableResID);
  73.     FAIL_OSERR (theErr, "\pFailed to copy the color table to the destination file")
  74.     
  75.     // get the color table
  76.     gAppColorTable = GetCTable( kAppColorTableResID );
  77.     FAIL_NIL (gAppColorTable, "\pFailed to open the color table")
  78.  
  79.     // determine the number of icon resources
  80.     numPicts = Count1Resources( 'PICT' );
  81.     
  82.     // get each one,
  83.     for( loop = 1; loop <= numPicts; loop++ )
  84.     {
  85.     // *********************************************************************
  86.     // Load the PICT and get the resource information
  87.     // *********************************************************************
  88.         UseResFile (inputFileResNum);
  89.         pictIsResource = true;
  90.         thePicture = (PicHandle) Get1IndResource ('PICT', loop);
  91.         theErr = ResError();
  92.         FAIL_NIL (thePicture, "\pFailed to load the picture")
  93.         FAIL_OSERR (theErr, "\pFailed to load the picture")
  94.         
  95.         GetResInfo( (Handle) thePicture, &resID, &resType, resName );
  96.         theErr = ResError();
  97.         FAIL_OSERR( theErr, "\pFailed to get info on the resource")
  98.         
  99.     // *********************************************************************
  100.     // Create a GWorld, erase it, and draw our pictures and masks into it
  101.     // We'll have three images across, left to right, in the GWorld
  102.     // *********************************************************************
  103.         pictRect = (**thePicture).picFrame;
  104.         OffsetRect (&pictRect, -pictRect.left, -pictRect.top);
  105.         drawRect = pictRect;
  106.         pictRect.right *= 3;
  107.         offset = drawRect.right;
  108.     
  109.         theErr = NewGWorld(&pictGWorld, kPreferredDepth, &pictRect, gAppColorTable, NULL, 0);
  110.         FAIL_OSERR (theErr, "\pFailed to allocate the GWorld")
  111.         FAIL_NIL (pictGWorld, "\pFailed to allocate the GWorld")
  112.     
  113.         pictPixMap  = GetGWorldPixMap(pictGWorld);
  114.         FAIL_NIL (pictPixMap, "\pCouldn't get the pixmap")
  115.         FAIL_FALSE ( LockPixels(pictPixMap), "\pCouldn't lock the pixmap")
  116.         
  117.         GetGWorld (&savePort, &saveDevice);
  118.         SetGWorld (pictGWorld, NULL);
  119.     
  120.         EraseRect (&pictRect);
  121.         DrawPicture (thePicture, &drawRect);
  122.         
  123.         sourceRect = drawRect;
  124.         
  125.         drawRect.left +=offset;
  126.         drawRect.right +=offset;
  127.         
  128.         // The top left corner of the picture is always assumed to be the Mask color!
  129.         // We set our mask color and insert our search proc, then we copy the mask.
  130.         
  131.         GetCPixel (0,0, &gMaskColor);
  132.         AddSearch (MaskSearchProcUPP);
  133.         CopyBits ((BitMap *) *pictPixMap, (BitMap *) *pictPixMap,
  134.                 &sourceRect, &drawRect, srcCopy, NULL);
  135.         DelSearch (MaskSearchProcUPP);
  136.         
  137.         sourceRect = drawRect;
  138.         drawRect.left+=offset;
  139.         drawRect.right+=offset;
  140.         
  141.         // The second is just a copy of the first.  No search is necessary
  142.         CopyBits ((BitMap *) *pictPixMap, (BitMap *) *pictPixMap,
  143.         &sourceRect, &drawRect, srcCopy, NULL);
  144.  
  145.     // *********************************************************************
  146.     // We're done with the old picture, so we can release it and build the
  147.     // new picture to add to the output file.
  148.     // *********************************************************************
  149.         ReleaseResource ((Handle) thePicture);
  150.         thePicture = NULL;
  151.     
  152.         pictIsResource = false;
  153.         
  154.         theParams.srcRect = pictRect;
  155.         theParams.hRes = 0x00480000;
  156.         theParams.vRes = 0x00480000;
  157.         theParams.version = -2;
  158.         theParams.reserved1 = 0;
  159.         theParams.reserved2 = 0;
  160.     
  161.         thePicture = OpenCPicture (&theParams);
  162.         ClipRect (&pictRect);
  163.         CopyBits ((BitMap *) *pictPixMap, (BitMap *) *pictPixMap, &pictRect, &pictRect, srcCopy, NULL);
  164.         ClosePicture();
  165.         theErr = QDError();
  166.         FAIL_NIL (thePicture, "\pFailed to create the new picture")
  167.         FAIL_OSERR (theErr, "\pFailed to create the new picture")
  168.         
  169.         SetGWorld (savePort, saveDevice);
  170.     
  171.     // *********************************************************************
  172.     // Add the new PICT to the resource file
  173.     // *********************************************************************
  174.     // We're done with the GWorld
  175.         DisposeGWorld (pictGWorld);
  176.         pictGWorld = NULL;
  177.     
  178.         UseResFile (outputFileResNum);
  179.         AddResource( (Handle) thePicture, 'PICT', resID, resName );
  180.         theErr = ResError();
  181.         FAIL_OSERR (theErr,"\pFailed to add the resource to the output file")
  182.     
  183.         pictIsResource = true;
  184.     
  185.         WriteResource( (Handle) thePicture );
  186.         theErr = ResError();
  187.         FAIL_OSERR (theErr,"\pFailed to write the resource to the output file")
  188.     
  189.         ReleaseResource( (Handle) thePicture );
  190.         thePicture = NULL;
  191.     }
  192.     
  193.     goto cleanup;
  194.     
  195. error:
  196.     
  197.     if (theErr == noErr)
  198.         theErr = paramErr;
  199.         
  200. cleanup:
  201.     
  202.     UseResFile (saveResNum);
  203.     
  204.     if (pictGWorld != NULL)
  205.         DisposeGWorld (pictGWorld);
  206.     
  207.     if (thePicture != NULL)
  208.         if (pictIsResource)
  209.             ReleaseResource ((Handle) thePicture);
  210.         else
  211.             DisposeHandle ((Handle) thePicture);
  212.     
  213.     if (gAppColorTable != NULL)
  214.         DisposeCTable (gAppColorTable);
  215.     gAppColorTable = NULL;
  216.     return theErr;
  217. }
  218.